package Server.controller.mobile;
import Plug.result.RetResponse;
import Plug.result.RetResult;
import Server.dao.ShopOrderDao;
import Server.entity.ShopOrder;
import Server.service.WxPayOrderService;
import org.jdom.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("WxController")
public class WxController {
    @Autowired
    private ShopOrderDao shopOrderDao;
    @Autowired
    private HttpServletRequest request;
    /**
     * 返回成功xml
     */
    public static final String RES_SUCCESS_XML = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";

    /**
     * 返回失败xml
     */
    public static final String RES_FAIL_XML = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>";

    @Autowired
    private WxPayOrderService wxPayOrderService;

    @RequestMapping("Pay")
    public RetResult Pay(@RequestBody ShopOrder shopOrder, HttpServletResponse response) throws IOException, JDOMException {
        shopOrder = shopOrderDao.findByUuid(shopOrder.getUuid());
        if (!shopOrder.getStatus().equals(ShopOrder.Status.NO)) {
            return RetResponse.makeErrRsp("当前状态不允许付款");
        } else {
            return RetResponse.makeOKRsp(wxPayOrderService.h5CreatePayQrCode(shopOrder.getUuid(), String.valueOf(shopOrder.getMoney().longValue()*100)));
        }
    }
    @RequestMapping("notifyWx")
    public String weChatNotifyUrl(@RequestBody String notify) {

        return RES_SUCCESS_XML;
    }
}
